package com.wg.user.util;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class LogUtil {

    public static Logger getDefaultLog() {
        return LoggerFactory.getLogger("defaultLog");
    }

    public static void exception(Exception e){
        if(null != e){
            getDefaultLog().error(e.getLocalizedMessage(), e);
        }
    }

    public static void exception(String msg,Exception e){
        if(null != e){
            getDefaultLog().error(msg, e);
        }
    }

    public static void exception(Exception e,String msg,Object... obj){
        if(obj == null || obj.length < 1){
            exception(msg,e);
            return;
        }
        List<String> strs = new ArrayList<String>();
        for (Object o : obj){
            try {
                strs.add(JSON.toJSONString(o));
            }catch (Exception excep){
                strs.add(o == null ? "" : o.toString());
                LogUtil.exception(excep);
            }
        }
        getDefaultLog().error(msg, strs.toArray());
        if(null != e){
            getDefaultLog().error(e.getLocalizedMessage(), e);
        }
    }

    public static void exception(String msg){
        getDefaultLog().error(msg);
    }
}
